<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BWeMember
 *
 * @author jianfeng
 */
class BWeCafe extends BApp {

    public function run($data = NULL) {
        parent::run($data);
        $cooker = new BAEventCooker();

        $preloadDataList = $this->getAppPreloadEventListData();
        foreach ($preloadDataList as $preloadData) {
            $cooker->preloadEvent($preloadData);
        }

        /*
          $cooker->addEventType('weBill', '买单功能');
          $cooker->addEventType('memberPoint', '积分功能');
          $cooker->addEventType('memberCard', '会员卡');
          $cooker->addEventType('membership', '会员资格');
          $cooker->addEventType('voucher', '代金券');
          $cooker->addEventType('wePoll', '微投票');
         * 
         */

        //register child control
        $screen = Blyn::app()->getUIManager()->getCurrentScreen();
        $appAdminControl = new BUIAppAdmin('appAdmin');
        $screen->addChildControl($appAdminControl);

        //register membership control
        $membershipControl = new BUIMembership('membershipControl');
        $screen->addChildControl($membershipControl);

        //register user control
        $userControl = new BUIUser('userControl');
        $screen->addChildControl($userControl);

        //register role control
        $roleControl = new BUIRole('roleControl');
        $screen->addChildControl($roleControl);

        //register attribute control
        $attributeControl = new BUIAttribute('attributeControl');
        $screen->addChildControl($attributeControl);

        //register category control
        $categoryControl = new BUICategory('categoryControl');
        $screen->addChildControl($categoryControl);

        //add billyn memberPointType, this is a basic memberPointType
        /*
          $memberPointCooker = new BAMemberPointCooker();
          $memberPointCooker->addMemberPointType('BMemberPointType', '比邻积分', array(
          'currency' => 'rmb',
          'currencyUnit' => 'fen',
          'currencyRate' => '1'//one point = one fen
          ));
         * 
         */

        //add permit for addPoint/deductPoint for BWeMember::manager::memberPoint
        //$memberPointCooker->addActionPermit('BWeMember_billyMemberPointType', BAMemberPointCooker::Action_AddPoint, BAMemberPointCooker::Permit_Allow);
        //$sender = Blyn::app()->getUIManager()->getSenderControl();

        /*
          if(Blyn::app()->getUIManager()->getUpdateControl() instanceof BUIScreen){
          $eventControl = new BUIEventControl();
          $screen->addChildControl($eventControl);
          }
         * 
         */

        /*
          if ($sender instanceof BUIOperation) {
          $screen = Blyn::app()->getUIManager()->getCurrentScreen();
          $cooker->onShowEventList($screen);
          }
         * 
         */
        //在非繁忙时间执行这个比较消耗时间的操作
        $hour = date('H');
        if ($hour > 15 & $hour < 22) {
            $runDate = Blyn::app()->getActiveSpace()->getCache('runDate_sendWechatMessageForApplyVoucher');
            if ($runDate == NULL || $runDate != date('Ymd')) {
                BAVoucherCooker::sendWechatMessageForApplyBirthVoucher();
                BAVoucherCooker::sendWechatMessageForLongBillIntervalVoucher();
                Blyn::app()->getActiveSpace()->setCache('runDate_sendWechatMessageForApplyVoucher', date('Ymd'));
            }
        }
    }

    public function runEventPanel() {
        $sender = Blyn::app()->getUIManager()->getSenderControl();
        $cooker = new BAEventCooker();
        if ($sender instanceof BUIOperation) {
            $screen = Blyn::app()->getUIManager()->getCurrentScreen();
            $cooker->onShowEventList($screen);
        }
    }

    public function getAppEventTypeListData() {
        $eventCooker = new BAEventCooker();
        $listData = array(
            'appEvent.wePoll' => '微投票',
        );

        return $eventCooker->populateAvailableEventTypeListData($listData);
    }

    public function getAppPreloadEventListData() {
        return array(
            /*
            array(
                'type' => array('systemEvent.eventShare' => '功能共享'),
                'event' => array(
                    'name' => 'eventShare',
                    'alias' => '功能分享',
                    'roles' => array(array(
                            'name' => BRole::Role_Space_Admin,
                            'allowModify' => 'no',
                            'allowDelete' => 'no',
                            'eventHome' => 'admin',
                            'eventHomeTitle' => '设置共享',
                        )
                    )
                ),
            ),
             * 
             */
            /*
            array(
                'type' => array('event.weChat' => '微信功能'),
                'event' => array(
                    'name' => 'weChat',
                    'alias' => '微信功能',
                    'roles' => array(array(
                            'name' => BRole::Role_Space_Admin,
                            'allowModify' => 'no',
                            'allowDelete' => 'no',
                            'eventHome' => 'admin',
                            'eventHomeTitle' => '设置微信',
                        )
                    )
                ),
            ),
             * 
             */
            array(
                'type' => array('appEvent.weBill' => '买单功能'),
                'event' => array(
                    'name' => 'billEvent',
                    'alias' => '买单功能',
                    'description' => '买单功能',
                    'roles' => array(
                        array(
                            'name' => BRole::Role_Space_Admin,
                            'eventHome' => 'admin',
                            'eventHomeTitle' => '设置买单',
                            'allowModify' => 'yes',
                        ),
                        array(
                            'name' => BRole::Role_Space_Member,
                            'eventHome' => 'manager',
                            'eventHomeTitle' => '买单管理',
                        )
                    )
                ),
            )
        );
    }
    
    public function getEventHomeList(){
        return array(
            'admin'=>'admin',
            'manager'=>'manager',
            'customer'=>'customer'
        );
    }

}
